home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / sox_2 / sox / c / cdr < prev    next >
Text File  |  1993-09-23  |  2KB  |  134 lines

  1. /*
  2.  * July 5, 1991
  3.  * Copyright 1991 Lance Norskog And Sundry Contributors
  4.  * This source code is freely redistributable and may be used for
  5.  * any purpose.  This copyright notice must be maintained. 
  6.  * Lance Norskog And Sundry Contributors are not responsible for 
  7.  * the consequences of using this software.
  8.  */
  9.  
  10. /*
  11.  * CD-R format handler
  12.  *
  13.  * David Elliott, Sony Microsystems
  14.  *
  15.  * This code automatically handles endianness differences
  16.  */
  17.  
  18. #include "st.h"
  19.  
  20. #define SECTORSIZE    (2352 / 2)
  21.  
  22. /* Private data for SKEL file */
  23. typedef struct cdrstuff {
  24.     int    samples;    /* number of samples written */
  25. } *cdr_t;
  26.  
  27. IMPORT float volume, amplitude;
  28. IMPORT int summary, verbose;
  29.  
  30. /*
  31.  * Do anything required before you start reading samples.
  32.  * Read file header. 
  33.  *    Find out sampling rate, 
  34.  *    size and style of samples, 
  35.  *    mono/stereo/quad.
  36.  */
  37.  
  38. cdrstartread(ft) 
  39. ft_t ft;
  40. {
  41.  
  42.     int     littlendian = 1;
  43.     char    *endptr;
  44.  
  45.     endptr = (char *) &littlendian;
  46.     if (!*endptr) ft->swap = 1;
  47.  
  48.     ft->info.rate = 44100;
  49.     ft->info.size = WORD;
  50.     ft->info.style = SIGN2;
  51.     ft->info.channels = 2;
  52.     ft->comment = NULL;
  53. }
  54.  
  55. /*
  56.  * Read up to len samples from file.
  57.  * Convert to signed longs.
  58.  * Place in buf[].
  59.  * Return number of samples read.
  60.  */
  61.  
  62. cdrread(ft, buf, len) 
  63. ft_t ft;
  64. long *buf, len;
  65. {
  66.  
  67.     return rawread(ft, buf, len);
  68. }
  69.  
  70. /*
  71.  * Do anything required when you stop reading samples.  
  72.  * Don't close input file! 
  73.  */
  74. cdrstopread(ft) 
  75. ft_t ft;
  76. {
  77. }
  78.  
  79. cdrstartwrite(ft) 
  80. ft_t ft;
  81. {
  82.     cdr_t cdr = (cdr_t) ft->priv;
  83.  
  84.     int     littlendian = 1;
  85.     char    *endptr;
  86.  
  87.     endptr = (char *) &littlendian;
  88.     if (!*endptr) ft->swap = 1;
  89.  
  90.     cdr->samples = 0;
  91.  
  92.     ft->info.rate = 44100;
  93.     ft->info.size = WORD;
  94.     ft->info.style = SIGN2;
  95.     ft->info.channels = 2;
  96.     
  97. }
  98.  
  99. cdrwrite(ft, buf, len) 
  100. ft_t ft;
  101. long *buf, len;
  102. {
  103.     cdr_t cdr = (cdr_t) ft->priv;
  104.  
  105.     cdr->samples += len;
  106.  
  107.     rawwrite(ft, buf, len);
  108. }
  109.  
  110. /*
  111.  * A CD-R file needs to be padded to SECTORSIZE, which is in terms of
  112.  * samples.  We write -32768 for each sample to pad it out.
  113.  */
  114.  
  115. cdrstopwrite(ft) 
  116. ft_t ft;
  117. {
  118.     cdr_t cdr = (cdr_t) ft->priv;
  119.     int padsamps = SECTORSIZE - (cdr->samples % SECTORSIZE);
  120.     short pad[2];
  121.  
  122.     pad[0] = 0;
  123.  
  124.     if (padsamps == SECTORSIZE) {
  125.         return;
  126.     }
  127.  
  128.     while (padsamps > 0) {
  129.         rawwrite(ft, pad, 1);
  130.         padsamps--;
  131.     }
  132. }
  133.  
  134.